//
//  LoginViewController.m
//  ReciteBook
//
//  Created by xuziyuan on 13-12-22.
//  Copyright (c) 2013年 student. All rights reserved.
//

#import "LoginViewController.h"
#import "User.h"
@interface LoginViewController ()


@end

static int iduser=1000;

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=YES;
    
}


- (IBAction)backgroundTop:(id)sender
{
    [self.uesrNameTextField resignFirstResponder];
    [self.emailTextField resignFirstResponder];
    [self.passwordsTextField resignFirstResponder];
    [self.comPasswordsTextField resignFirstResponder];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.myAppDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finish)];
    self.navigationItem.rightBarButtonItem=rightBtn;

    
    [self queryUser];
}

//注册完成发送消息
- (void)finish
{
    NSString *tmp=@"";
    if ([self.uesrNameTextField.text isEqualToString:tmp]||[self.emailTextField.text isEqualToString:tmp]||[self.passwordsTextField.text isEqualToString:tmp]||[self.comPasswordsTextField.text isEqualToString:tmp]) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"选项信息不能为空" message:@"请填写完整" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
        [alert show];
    }else{
        if (![self.passwordsTextField.text isEqualToString:self.comPasswordsTextField.text]) {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"密码不相符合" message:@"请重新确认" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
            [alert show];
        }else{
            int flag=0;
            for (User *user in self.listUser) {
                if ([user.emailAdress isEqualToString:self.emailTextField.text]) {
                    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"邮箱已经使用过" message:@"请重新确认" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
                    [alert show];
                    flag=1;
                    break;
                }
            }
            if (flag==0) {
                User *newuser=(User *)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.myAppDelegate.managedObjectContext];
                newuser.idUser=[NSString stringWithFormat:@"%d",[self addidcount]];
                newuser.nameUser=self.uesrNameTextField.text;
                newuser.emailAdress=self.emailTextField.text;
                newuser.password=self.passwordsTextField.text;
                NSLog(@"iduser=%@,name=%@,email=%@,password=%@",newuser.idUser,newuser.nameUser,newuser.emailAdress,newuser.password);
                self.uesrNameTextField.text=@"";
                self.emailTextField.text=@"";
                self.passwordsTextField.text=@"";
                self.comPasswordsTextField.text=@"";
                [self queryUser];
                
                NSError *error;
                if (![self.myAppDelegate.managedObjectContext save:&error]) {
                    NSLog(@"保存成功");
                }else {
                    NSLog(@"%@ %@",error,error.userInfo);
                }
            }
        }
    }
}

- (int)addidcount
{
    return iduser++;
}

- (void)queryUser
{
    NSEntityDescription *entityUser=[NSEntityDescription entityForName:@"User" inManagedObjectContext:self.myAppDelegate.managedObjectContext];
    NSFetchRequest *request=[[NSFetchRequest alloc] init];
    
    [request setEntity:entityUser];
    self.listUser=[[self.myAppDelegate.managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
